package com.harmontronics.hanma.paas.paas.config;


import com.harmontronics.hanma.paas.paas.domain.releasesetup.entity.valueobject.HmReleaseSetupFields;
import com.harmontronics.hanma.paas.paas.interceptor.AppDataSourceLoadInterceptor;
import com.harmontronics.hanma.paas.paas.interceptor.ReportDataSourceInterceptor;
import com.harmontronics.hanma.paas.paas.interceptor.TokenValidInterceptor;
import com.harmontronics.mos.sdk.TokenRefreshAndValidInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * MVC配置
 *
 * @date 2017-04-20 22:30
 */
@Configuration
@ConditionalOnClass(name = "com.harmontronics.hanma.paas.paas.interceptor.TokenValidInterceptor")
public class WebMvcConfig implements WebMvcConfigurer {

  @Value("${local.imgfile.prefix}")
  private String prefix;

  @Value("${local.imgfile.localWinAddress}")
  private String localWinAddress;

  @Value("${local.imgfile.localLinuxAddress}")
  private String localLinuxAddress;

  @Value("${mos.oss.enabled:false}")
  private Boolean mosEnabled;

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {

    String localAddress = "";
    if(System.getProperty("os.name").toLowerCase().contains("windows")) {
      localAddress = localWinAddress;
    }else{
      //若当前系统是linux系统
      localAddress = localLinuxAddress;
    }
    registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    registry.addResourceHandler(prefix).addResourceLocations("file:" + localAddress);

    registry.addResourceHandler("/release/**").addResourceLocations("file:" + getPackPath());
  }

  private String getPackPath(){
    ApplicationHome applicationHome = new ApplicationHome(getClass());
    File homeDir = applicationHome.getDir();
    File dir = new File(homeDir, HmReleaseSetupFields.PACK_ROOT_DIR);
    return dir.getAbsolutePath();
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {

    if (mosEnabled != null && mosEnabled) {
      registry.addInterceptor(tokenRefreshAndValidInterceptor())
              .addPathPatterns("/api/**");
    } else {
      registry.addInterceptor(tokenValidInterceptor())
              .addPathPatterns("/api/**");
    }
    registry.addInterceptor(appDataSourceLoadInterceptor())
            .addPathPatterns("/api/**");

    registry.addInterceptor(reportDataSourceInterceptor())
            .addPathPatterns("/api/hmureport/**");
  }

  @Bean
  public TokenValidInterceptor tokenValidInterceptor(){

    return new TokenValidInterceptor();
  }

  @Bean
  public AppDataSourceLoadInterceptor appDataSourceLoadInterceptor(){
    return new AppDataSourceLoadInterceptor();
  }

  @Bean
  public ReportDataSourceInterceptor reportDataSourceInterceptor(){

    return new ReportDataSourceInterceptor();
  }

  @Bean
  public TokenRefreshAndValidInterceptor tokenRefreshAndValidInterceptor() {
    return new TokenRefreshAndValidInterceptor("hanma");
  }

 }
